package com.yuerlive.rainbow.module.playlive.presenter;

import com.google.gson.Gson;
import com.yuerlive.rainbow.api.EditApi;
import com.yuerlive.rainbow.api.EditService;
import com.yuerlive.rainbow.api.LiveApi;
import com.yuerlive.rainbow.api.LiveService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.GameInfo;
import com.yuerlive.rainbow.entity.KaiboData;
import com.yuerlive.rainbow.entity.LiveDetailData;
import com.yuerlive.rainbow.entity.StreamData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.module.main.presenter.MainContract;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/6/9.
 */

public class PlayLiveRoomPresenter extends BasePresenterImpl implements PlayLiveRoomContract.Presenter {
    private EditService editService;
    private PlayLiveRoomContract.View view;
    private LiveService liveService;
    private static String TAG = "PlayLiveRoomPresenter";

    public PlayLiveRoomPresenter(PlayLiveRoomContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        editService = EditApi.getInstance().service;
        this.liveService = LiveApi.getInstance().liveService;
    }

    @Override
    public void startLive() {
        Map<String, String> params = new HashMap<>();

        liveService.startLive(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<KaiboData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<KaiboData> baseData) {
                        if (baseData.getCode() == 0) {
                            view.stremDataReturn(baseData);
                        } else {
                            view.startLiveFaild(baseData.getCode(), baseData.getResult());
                        }
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void liveNotify(String gameId) {
        Map<String, String> params = new HashMap<>();
        params.put("gameId", gameId);

        liveService.sendLiveNotify(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData liveDetailDataBaseData) {
                        L.e(TAG, "liveDetailDataBaseData " + liveDetailDataBaseData.getResult());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getGameList() {
        liveService.getGameList().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<GameInfo>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<GameInfo>> listBaseData) {
                        view.showGameList(listBaseData.getObject());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void setLiveIcon(String url) {
        Map<String, String> params = new HashMap<>();
        params.put("setIcon", url);
        User user = UserProxy.getUser();
        params.put("id", user.getUserInfo().getLiveId());
        liveService.setLiveIcon(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        L.e(TAG, baseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }


}
